﻿namespace ConsoleApp1
{

    public abstract class Shape
    {
        public abstract double CalculateArea();
        public abstract bool IsValid();
    }

    public class Rectangle : Shape
    {
        public double Length { get; }
        public double Width { get; }

        public Rectangle(double length, double width)
        {
            Length = length;
            Width = width;
        }

        public override double CalculateArea() => Length * Width;
        public override bool IsValid() => Length > 0 && Width > 0;
    }


    public class Square : Shape
    {
        public double Side { get; }

        public Square(double side) => Side = side;

        public override double CalculateArea() => Side * Side;
        public override bool IsValid() => Side > 0;
    }

    public class Circle : Shape
    {
        public double Radius { get; }

        public Circle(double radius) => Radius = radius;

        public override double CalculateArea() => Math.PI * Radius * Radius;
        public override bool IsValid() => Radius > 0;
    }

    public static class ShapeFactory
    {

        public static Shape CreateRandomShape()
        {
            Random random = new();
            return random.Next(0, 3) switch
            {
                0 => new Rectangle(random.NextDouble() * 10, random.NextDouble() * 10),
                1 => new Square(random.NextDouble() * 10),
                2 => new Circle(random.NextDouble() * 10),
                _ => throw new InvalidOperationException()
            };
        }
    }



    internal class Program
    {
        static void Main(string[] args)
        {
            var shapes = new List<Shape>();
            for (var i = 0; i < 10; i++)
            {
                shapes.Add(ShapeFactory.CreateRandomShape());
            }

            double totalArea = shapes.Where(s => s.IsValid())
                                    .Sum(s => s.CalculateArea());

            Console.WriteLine($"合法形状总面积：{totalArea:F2}");
        }
    }
}
